package com.supermarket.api.web.admin;

import com.supermarket.api.bean.order.*;
import com.supermarket.api.common.BaseResponse;
import com.supermarket.api.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 订单接口
 * @author 李其伟
 * @date 2023/11/11
 */
@Controller
@RequestMapping("/admin/order")
public class AdminOrderController {

    /**
     * 订单服务层
     */
    @Autowired
    private OrderService orderService;

    @ResponseBody
    @PostMapping("/confirm")
    public String confirmOrder(@Valid @RequestBody ConfirmOrderReq req){
        List<ProductItem> res = orderService.confirmOrder(req);
        return BaseResponse.successJson(res);
    }

    @ResponseBody
    @PostMapping("/generate")
    public String generateOrder(@Valid @RequestBody GenerateOrderReq req){
        String orderSn = orderService.generateOrder(req);
        return BaseResponse.successJson(orderSn);
    }

    @ResponseBody
    @GetMapping("/list")
    public String list(OrderReq req){
        Page<OrderRes> resPage = orderService.list(req);
        return BaseResponse.successJson(resPage);
    }

    @ResponseBody
    @GetMapping("/detail")
    public String detail(OrderSn req){
        OrderRes res = orderService.detail(req.getOrderSn());
        return BaseResponse.successJson(res);
    }

    @ResponseBody
    @PostMapping("/paySuccess")
    public String paySuccess(@Valid @RequestBody PaySuccessReq req){
        orderService.paySuccess(req);
        return BaseResponse.successJson();
    }

    @ResponseBody
    @PostMapping("/cancelOrder")
    public String cancelOrder(@Valid @RequestBody OrderSn req){
        orderService.cancelOrder(req.getOrderSn());
        return BaseResponse.successJson();
    }

    @ResponseBody
    @PostMapping("/deleteOrder")
    public String deleteOrder(@Valid @RequestBody OrderSn req){
        orderService.deleteOrder(req.getOrderSn());
        return BaseResponse.successJson();
    }

}
